##  This file contains the code to replicate the analyses reported in Table SI-1
##

rm(list = ls(all.names = TRUE))
library(Hmisc)
library(lmtest)
library(sandwich)
library(pscl)
library(MASS)
library(mice)
library(miceadds)
library(openxlsx)


#setwd("~/Dropbox/Current projects/Nazi trials/analysis")
load("Nazi trials replication data.RData")
file.info("Nazi trials replication data.RData")$mtime


##  source helper functions
source("nb2.r")



##  deal with missing outcome data for truncated negative binomial
cases.hurdle <- cases.temp
cases.hurdle$Months2[is.na(cases.hurdle$Months2)] <- 0

sel <- cases.temp$convicted == TRUE & is.na(cases.temp$Months2)
table(sel)
cases.hurdle <- cases.hurdle[!sel,]
dim(cases.hurdle)



##
##  Estimate models and save the results
tab1 <- matrix("",29,6)
colnames(tab1) <- paste0("(",1:ncol(tab1),")")

rownames(tab1) <- c(
					"% judges who were appointed in 1933--45", "",
					"% judges who were appointed pre-1933", "",
					"% judges who completed legal training in 1933--45", "",
					"% judges who completed legal training pre-1933", "",
					"avg. share of judges' legal education in 1933--45", "",
					"avg. share of judges' legal education pre-1933", "",
					"% prosecutors who were appointed in 1933--45", "",
					"% prosecutors who were appointed pre-1933", "",
					"% prosecutors who completed legal training in 1933--45", "",
					"% prosecutors who completed legal training pre-1933", "",
					"avg. share of prosecutors' legal education in 1933--45", "",
					"avg. share of prosecutors' legal education pre-1933", "",
					"Year FE","State FE", "Crime categorization FE",
					"$p$-value Wald test % judges","$p$-value Wald test % prosecutors")

sims.tab <- vector(mode = "list", length = 6)


##  add additional predictors?
add.pred <- " + state"	#  state FEs; all models include crime categories and year FEs



##  col 1
add.pred.MI <- c("NS_judge_@_20","NS_prosecutor_@_20")

##  probit model
out <- nb.MI(d = cases.hurdle, outcome = "Months2", add.pred.MI = add.pred.MI, add.pred = add.pred,
wald = NULL)
tab1[c(1,13),1] <- out[[1]][2:3,1]
tab1[c(2,14),1] <- out[[1]][2:3,2]
sims.tab[[1]][1] <- list(out[[3]])



##  col 2
add.pred.MI <- c("NS_judge_@_20","NS_prosecutor_@_20","NS_judge_@_19","NS_prosecutor_@_19")

##  probit
out <- nb.MI(d = cases.hurdle, outcome = "Months2", add.pred.MI = add.pred.MI, add.pred = add.pred,
wald = list(c(1,3),c(2,4)))
tab1[c(1,13,3,15),2] <- out[[1]][2:5,1]
tab1[c(2,14,4,16),2] <- out[[1]][2:5,2]
tab1[28,2] <- out[[2]][1]
tab1[29,2] <- out[[2]][2]



##  col 5
add.pred.MI <- c("NS_judge_@_24","NS_prosecutor_@_24")

##  probit
out <- nb.MI(d = cases.hurdle, outcome = "Months2", add.pred.MI = add.pred.MI, add.pred = add.pred,
wald = NULL)
tab1[c(9,21),5] <- out[[1]][2:3,1]
tab1[c(10,22),5] <- out[[1]][2:3,2]



##  col 6
add.pred.MI <- c("NS_judge_@_24","NS_prosecutor_@_24","NS_judge_@_23","NS_prosecutor_@_23")

##  probit
out <- nb.MI(d = cases.hurdle, outcome = "Months2", add.pred.MI = add.pred.MI, add.pred = add.pred,
wald = list(c(1,3),c(2,4)))
tab1[c(9,21,11,23),6] <- out[[1]][2:5,1]
tab1[c(10,22,12,24),6] <- out[[1]][2:5,2]
tab1[28,6] <- out[[2]][1]
tab1[29,6] <- out[[2]][2]



##  add checkmarks to table
tab1[25:27,1:6]	<- "\\checkmark"


##  extract only the relevant parts of the results table
tab1[c(1:4,9:16,21:29),c(1:2,5:6)]

latex(tab1[c(1:4,9:16,21:29),c(1:2,5:6)], file = "")


sessionInfo()




.
